#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <list>
using namespace std;
#include "../../../NetEngine_Linux/NetEngine_SourceCode/XyRyNet_CommHdr.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/XyRyNet_Types.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/XyRyNet_ProtocolHdr.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/NetEngine/NetEngine_Core/NetCore_Define.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/NetEngine/NetEngine_Core/NetCore_Error.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/NetEngine_RfcComponents/RfcComponents_HttpServer/HttpServer_Define.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/NetEngine_RfcComponents/RfcComponents_HttpServer/HttpServer_Error.h"

//g++ -std=gnu++11 -Wall -g Test_HTTPServer.cpp -o Test_HTTPServer -L ../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_BaseLib -L ../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_Core -L ../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_RfcComponents -lNetEngine_BaseLib -lNetEngine_Core -lRfcComponents_HttpServer -lpthread -Wl,-rpath=../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_BaseLib:../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_Core:../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_RfcComponents,--disable-new-dtags

XNETHANDLE xhToken;
BOOL __stdcall NetCore_CB_Login(LPCSTR lpszClientAddr, SOCKET hSocket, LPVOID lParam)
{
    printf("NetCore_CB_Login:%s\n", lpszClientAddr);
    RfcComponents_HttpServer_CreateClient(lpszClientAddr);
    return TRUE;
}
void __stdcall NetCore_CB_Recv(LPCSTR lpszClientAddr, SOCKET hSocket, LPCSTR lpszRecvMsg, int nMsgLen, LPVOID lParam)
{
    printf("NetCore_CB_Recv:%s-%d\n", lpszClientAddr, nMsgLen);
    if (!RfcComponents_HttpServer_InserQueue(lpszClientAddr, lpszRecvMsg, nMsgLen))
    {
        printf("RfcComponents_WSPacket_Post:%lX\n", HttpServer_GetLastError());
    }
}
void __stdcall NetCore_CB_Close(LPCSTR lpszClientAddr, SOCKET hSocket, LPVOID lParam)
{
    printf("NetCore_CB_Close:%s\n", lpszClientAddr);
    RfcComponents_HttpServer_CloseClinet(lpszClientAddr);
}

LPVOID NetCore_Thread(LPVOID lParam)
{
    while (TRUE)
    {
        if (RfcComponents_HttpServer_EventWait())
        {
            int nMsgLen = 20480;
            RFCCOMPONENTS_HTTP_REQPARAM st_ReqParam;
            TCHAR tszMsgBuffer[20480];
            TCHAR tszClientAddr[64];
            list<tstring> stl_ListHdrField;

            memset(&st_ReqParam, '\0', sizeof(RFCCOMPONENTS_HTTP_REQPARAM));
            memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));
            memset(tszClientAddr, '\0', sizeof(tszClientAddr));

            if (RfcComponents_HttpServer_GetRandom(tszClientAddr, &stl_ListHdrField, &st_ReqParam, tszMsgBuffer, &nMsgLen))
            {
                printf("%s %d:%s\n", tszClientAddr, nMsgLen, tszMsgBuffer);
                RFCCOMPONENTS_HTTP_HDRPARAM st_HdrParam;
                memset(&st_HdrParam, '\0', sizeof(RFCCOMPONENTS_HTTP_HDRPARAM));

                st_HdrParam.bIsClose = TRUE;
                st_HdrParam.nHttpCode = 200;
                RfcComponents_HttpServer_SendMsg(tszMsgBuffer, &nMsgLen, &st_HdrParam);
                NetCore_TCPEPoll_SendEx(xhToken, tszClientAddr, tszMsgBuffer, nMsgLen);
            }
        }
        Sleep(1);
    }
    return 0;
}

int main()
{
    LPCTSTR lpszMiniFile = _T("HttpMime.types");
    LPCTSTR lpszCodeFile = _T("HttpCode.types");

    if (!RfcComponents_HttpServer_Init(lpszCodeFile,lpszMiniFile))
    {
        printf("RfcComponents_HttpServer_Init:%lX\n", HttpServer_GetLastError());
        return 0;
    }
    if (!NetCore_TCPEPoll_StartEx(&xhToken))
    {
        printf("NetCore_TCPEPoll_StartEx:%lX\n", NetCore_GetLastError());
        return 0;
    }

    NetCore_TCPEPoll_RegisterCallBackEx(xhToken, NetCore_CB_Login, NetCore_CB_Recv, NetCore_CB_Close);

    HANDLE hThread;
    pthread_create(&hThread, NULL, NetCore_Thread, NULL);
    while (1)
    {
        Sleep(1000);
    }
    NetCore_TCPEPoll_DestroyEx(xhToken);
    RfcComponents_HttpServer_Destroy();
    return 0;
}
